﻿using System;
using Microsoft.Practices.Unity;
using TSharp.Core.Osgi;
using TSharp.Core.Osgi.Internal;

namespace TSharp.Core.Unity
{
    partial class UnityServiceSituation
    {
        private static readonly object synobj = new object();
        [ThreadStatic]
        private static UnityLocatorWrapper _threadContainer;

        private IUnityContainer CreateThreadContainer()
        {
            log.Debug("创建 Thread");

            IUnityContainer container = GetOrCreateRootWrapper().unity.CreateChildContainer();

            UnitySection.Configure(container, CONFIG_CONTAINER_NAME_THREAD);

            Configure(ServiceManager.ThreadMap, container);
            return container;
        }


        private UnityLocatorWrapper GetOrCreateThreadWrapper()
        {
            if (_threadContainer != null)
                return _threadContainer;
            lock (synobj)
            {
                if (_threadContainer == null)
                    _threadContainer = new UnityLocatorWrapper(CreateThreadContainer(), Level.THREAD);
                return _threadContainer;
            }
        }
    }
}